/**
 * 
 */
package fr.pioug.gdocs.gui;

import java.awt.event.*;
import java.util.*;

import javax.swing.*;

import org.apache.commons.lang.*;
import org.jdesktop.jdic.tray.*;

import fr.pioug.gdocs.service.*;

/**
 * @author a011228
 * @version
 */
public class GDocsSync implements ActionListener, Observer {

	private SystemTray mTray = SystemTray.getDefaultSystemTray();

	private TrayIcon mTrayIcon;

	private GDocsSyncConfiguration mSyncConfiguration;

	private JPopupMenu mMenu;

	private JMenuItem mMenuItem;

	private static GDocsSync mGDocsSync = null;

	/**
	 * GUI Mode
	 * 
	 * @param pArgs
	 */
	public GDocsSync(String[] pArgs) {

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception lException) {
			LogService.error("error.lookAndFell", GDocsSync.class, lException);
		}
		System.setProperty("javax.swing.adjustPopupLocationToFit", "false");
		mMenu = new JPopupMenu(ResourceService.getInstance().getValue(
				"label.menu"));

		// "Configuration" menu item
		mMenuItem = new JMenuItem(ResourceService.getInstance().getValue(
				"label.configuration"));
		mMenuItem.addActionListener(this);
		mMenu.add(mMenuItem);

		// Separator
		mMenu.addSeparator();

		// "Synchro" menu item
		mMenuItem = new JMenuItem(ResourceService.getInstance().getValue(
				"label.synchro"));
		mMenuItem.addActionListener(this);
		mMenu.add(mMenuItem);

		// "Zip" menu item
		// mMenuItem = new JMenuItem(ResourceService.getInstance().getValue(
		// "label.zip"));
		// mMenuItem.addActionListener(this);
		// mMenu.add(mMenuItem);

		// "Quit" menu item
		mMenu.addSeparator();
		mMenuItem = new JMenuItem(ResourceService.getInstance().getValue(
				"label.quit"));
		mMenuItem.addActionListener(this);
		mMenu.add(mMenuItem);

		ImageIcon lImageIcon = new ImageIcon(GDocsSync.class
				.getResource("/images/tray.png"));
		mTrayIcon = new TrayIcon(lImageIcon, ResourceService.getInstance()
				.getValue("label.tray"), mMenu);

		mTrayIcon.setIconAutoSize(true);

		mSyncConfiguration = new GDocsSyncConfiguration(mTrayIcon);
		mTrayIcon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent pActionEvent) {
				mSyncConfiguration.setVisible(!mSyncConfiguration.isVisible());

			}
		});

		mTray.addTrayIcon(mTrayIcon);

	}

	/**
	 * @param e
	 */
	public void actionPerformed(ActionEvent e) {
		JMenuItem source = (JMenuItem) (e.getSource());
		String s = source.getText();
		if (s.equalsIgnoreCase(ResourceService.getInstance().getValue(
				"label.quit"))) {
			System.exit(0);
		} else if (s.equalsIgnoreCase(ResourceService.getInstance().getValue(
				"label.synchro"))) {
			mTrayIcon
					.displayMessage(ResourceService.getInstance().getValue(
							"message.title.start"), ResourceService
							.getInstance().getValue("message.desc.start"),
							TrayIcon.INFO_MESSAGE_TYPE);

			Thread lThread = new Thread(SyncOnServerService
					.getInstance(getInstance()));
			lThread.start();

		} else if (s.equalsIgnoreCase(ResourceService.getInstance().getValue(
				"label.configuration"))) {
			mSyncConfiguration.setVisible(true);

		} else if (s.equalsIgnoreCase(ResourceService.getInstance().getValue(
				"label.zip"))) {
			Thread lThread = new Thread(SyncZipService
					.getInstance(getInstance()));
			lThread.start();
		}

	}

	/**
	 * 
	 * @param pArgs
	 */
	public static void main(String[] pArgs) {
		if (pArgs != null && pArgs.length > 0) {
			mGDocsSync = new GDocsSync(pArgs);
			mGDocsSync.mSyncConfiguration.setVisible(false);
			new Thread(SyncOnServerService.getInstance(mGDocsSync)).run();
		} else {
			mGDocsSync = new GDocsSync(pArgs);
			mGDocsSync.mSyncConfiguration.setVisible(true);
		}
	}

	/**
	 * @param pO
	 * @param pArg
	 */
	public void update(Observable pO, Object pArg) {
		LogService.debug(String.valueOf(pArg), GDocsSync.class);
		mTrayIcon.setToolTip(String.valueOf(pArg));
		if (StringUtils.isEmpty(String.valueOf(pArg))) {
			mTrayIcon.displayMessage(ResourceService.getInstance().getValue(
					"message.title.finish"), ResourceService.getInstance()
					.getValue("message.desc.finish"),
					TrayIcon.INFO_MESSAGE_TYPE);
			LogService.info("message.desc.finish", GDocsSync.class);
			mTrayIcon.setToolTip(ResourceService.getInstance().getValue(
					"label.tray"));

		}

	}

	/**
	 * @return
	 */
	public static GDocsSync getInstance() {
		return mGDocsSync;
	}

}
